import ExpenseCard from "../pay-info-list/create-expense-card";
import React from "react";
import {Popover, Table} from "antd";
import {queryCurrencyPrecision, messages } from "share/common";
import AssociateContract from "../pay-info-list/associate-contract";
import openSlideFrame from "share/openSlideFrame";

let commonContext;
const prepaymentApplyColumns = [
    {
      title: messages('finance-9.key8')/*对公支付单号*/,
      dataIndex: 'businessCode',
      width: 150,
      render: (value, record) =>
        <Popover content={value} placement={'topLeft'}>
          <a onClick={() => openCorporateReportDetail(record)}>{value}</a>
        </Popover>
    }, {
    title: messages('common.currency')/*币种*/,
    dataIndex: 'currencyCode',
    width: 80
  }, {
    title: messages('finance-9.key9')/*核销金额*/,
    dataIndex: 'writeOffAmount',
    render: (value, record) => React.Component.prototype.filterMoney(value, queryCurrencyPrecision(record.currencyCode)),
    width: 200
  }
];

function formatBankNumber(value) {
  if(typeof value !== 'string') return value;
  return value.replace(/(^\d{4})(\d+)(\d{4}$)/, (target, $1, $2, $3) => {return $1 + $2.replace(/./g, '*') + $3})
};

function openCorporateReportDetail(record) {
  openSlideFrame.openCorporateReportDetail({
    expenseReportOID: record.prepaymentHeadOid,
    entry: 'view',
    readOnly: true
  }, {
    noPadding: true,
    className: 'corporate-payment-report-detail',
    style: {
      width: '100vw',
      height: '100vh',
      top: 0,
      zIndex: 999
    }
  }, commonContext);
}

function openContractDetail(record) {
  openSlideFrame.contractDetail({
    entityOID: record.contractOid,
    entry: 'view',
    id: record.contractHeaderId
  }, {
    className: 'corporate-payment-report-detail',
    style: {
      width: '100vw',
      height: '100vh',
      top: 0,
      zIndex: 999
    }
  }, commonContext);
}

export default function PayInfoCustomFields(context) {
  commonContext = context;
  return {
    'accountNumber': {
      label: messages('finance-9.key10')/*收款方账号*/,
      value: payInfoDetail => formatBankNumber(payInfoDetail.accountFullNumber || payInfoDetail.accountNumber)
    },
    'accountName': {
      label: messages('finance-9.key11')/*收款方户名*/,
      value: payInfoDetail => payInfoDetail.accountName
    },
    'payeeAccountNumber': {
      label: messages('finance-9.key10')/*收款方账号*/,
      value: payInfoDetail => payInfoDetail.payeeAccountNumber
    },
    'payeeAccountName': {
      label: messages('finance-9.key11')/*收款方户名*/,
      value: payInfoDetail => payInfoDetail.payeeAccountName
    },
    'invoiceViewDTO': {
      label: messages('finance-9.key12')/*费用信息*/,
      value: (payInfoDetail, expenseInfo) => (
        <ExpenseCard
          editable={false}
          applicantOID={expenseInfo.applicantOID}
          expenseInfo={expenseInfo}
          invoiceViewDTO={payInfoDetail.invoiceViewDTO}
        />
      )
    },
    'writeOffCommandList': {
      label: messages('finance-9.key13')/*核销明细*/,
      value: payInfoDetail => payInfoDetail.writeOffCommandList && payInfoDetail.writeOffCommandList.length ? (
        <Table
          rowKey="id"
          columns={prepaymentApplyColumns}
          dataSource={payInfoDetail.writeOffCommandList}
          size="small"
          bordered
          pagination={false}
        />
      ) : '-'
    },
    'associateCorpBusinessCode': {
      label: messages('finance-9.key14')/*关联前期发票*/,
      value: payInfoDetail => payInfoDetail.associateCorpBusinessCode
    },
    'prepaymentTypeCode': {
      label: messages('finance-9.key15')/*预付类型*/,
      value: payInfoDetail => payInfoDetail.prepaymentTypeName
    },
    'contractAssociatedDTOList': {
      label: messages('finance-9.key16')/*关联合同行*/,
      value: (payInfoDetail, expenseInfo) =>
        payInfoDetail.contractAssociatedDTOList && payInfoDetail.contractAssociatedDTOList[0] ? (
          <AssociateContract
            expenseInfo={expenseInfo}
            contract={payInfoDetail.contractAssociatedDTOList || []}
            editable={false}
            openContractDetail={openContractDetail}
          />
        ) : '-'
    },
    'planedAmount': {
      label: messages('finance-9.key17')/*本次支付金额*/,
      value: (payInfoDetail, expenseInfo) => expenseInfo.currencyCode +
        ' ' +
        React.Component.prototype.filterMoney(
          payInfoDetail.paymentScheduleType === 1003 ? payInfoDetail.amount : payInfoDetail.planedAmount,
          queryCurrencyPrecision(expenseInfo.currencyCode), true
        )
    },
    'paymentMethod': {
      label: messages('finance-9.key18')/*付款方式*/,
      value: (payInfoDetail, info, paymentMethodList) => {
        if(payInfoDetail.paymentScheduleType === 1003)
          return payInfoDetail.paymentMethodName || '-';
        let paymentMethod = paymentMethodList.filter(i => i.id === payInfoDetail.paymentMethodDetailId);
        return paymentMethod.length ? paymentMethod[0].description : '-'
      }
    },
    'planedPaymentDate': {
      label: messages('finance-9.key19')/*计划付款日期*/,
      value: payInfoDetail => new Date(payInfoDetail.planedPaymentDate).format('yyyy-MM-dd')
    },
    'remark': {
      label: messages('common.remark')/*备注*/,
      value: payInfoDetail => payInfoDetail.remark || '-'
    }
  }
}
